package com.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/api/stock")
@Slf4j
public class SampleController {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    // 获取逐渐递增的交易额和交易数量统计数据
    @GetMapping("/processing_speed")
    public String getMinuteStats(@RequestParam int number) {
        log.info("请求计算速度，速度是："+ redisTemplate.opsForList().index("processing_speed_",number));
        return (String) redisTemplate.opsForList().index("processing_speed_",number);
    }
@GetMapping("/tradeAndBuy")
public List<Double> getsellandbuy(@RequestParam int number) {
    // 获取当前下标的元素
    String currentStr = (String) redisTemplate.opsForList().index("2:", number);
    if (currentStr == null) {
        return List.of(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
    }
    String[] currentArray = currentStr.split(":");
    if (currentArray.length != 4) {
        log.error("数据格式不正确: currentArray={}", currentArray);
        return List.of(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
    }
    double currentTotalAmount = Double.parseDouble(currentArray[0]);
    double currentTotalVolumeCount = Double.parseDouble(currentArray[1]);
    double currentTotalBuyAmount = Double.parseDouble(currentArray[2]);
    double currentTotalSellAmount = Double.parseDouble(currentArray[3]);

    // 计算totalAmount和totalVolumeCount
    double totalAmount = currentTotalAmount;
    double totalVolumeCount = currentTotalVolumeCount;
    double totalBuyAmount = currentTotalBuyAmount;
    double totalSellAmount = currentTotalSellAmount;
    if (number >= 1) {
        String previousStr = (String) redisTemplate.opsForList().index("2:", number - 1);
        if (previousStr != null) {
            String[] previousArray = previousStr.split(":");
            if (previousArray.length != 4) {
                log.error("数据格式不正确: previousArray={}", previousArray);
                return List.of(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
            }
            double previousTotalAmount = Double.parseDouble(previousArray[0]);
            double previousTotalVolumeCount = Double.parseDouble(previousArray[1]);
            double previousTotalBuyAmount = Double.parseDouble(previousArray[2]);
            double previousTotalSellAmount = Double.parseDouble(previousArray[3]);
            totalAmount = currentTotalAmount - previousTotalAmount;
            totalVolumeCount = currentTotalVolumeCount - previousTotalVolumeCount;
            totalBuyAmount = currentTotalBuyAmount - previousTotalBuyAmount;
            totalSellAmount = currentTotalSellAmount - previousTotalSellAmount;
        }
    }

    // 计算minuteTotalAmount和minuteTotalVolumeCount
    double minuteTotalAmount = currentTotalAmount;
    double minuteTotalVolumeCount = currentTotalVolumeCount;
    double minuteTotalBuyAmount = currentTotalBuyAmount;
    double minuteTotalSellAmount = currentTotalSellAmount;
    if (number >= 60) {
        String previousMinuteStr = (String) redisTemplate.opsForList().index("2:", number - 60);
        if (previousMinuteStr != null) {
            String[] previousMinuteArray = previousMinuteStr.split(":");
            if (previousMinuteArray.length != 4) {
                log.error("数据格式不正确: previousMinuteArray={}", previousMinuteArray);
                return List.of(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
            }
            double previousMinuteTotalAmount = Double.parseDouble(previousMinuteArray[0]);
            double previousMinuteTotalVolumeCount = Double.parseDouble(previousMinuteArray[1]);
            double previousMinuteTotalBuyAmount = Double.parseDouble(previousMinuteArray[2]);
            double previousMinuteTotalSellAmount = Double.parseDouble(previousMinuteArray[3]);
            minuteTotalAmount = currentTotalAmount - previousMinuteTotalAmount;
            minuteTotalVolumeCount = currentTotalVolumeCount - previousMinuteTotalVolumeCount;
            minuteTotalBuyAmount = currentTotalBuyAmount - previousMinuteTotalBuyAmount;
            minuteTotalSellAmount = currentTotalSellAmount - previousMinuteTotalSellAmount;
        }
    }

    // 计算hourlyTotalAmount和hourlyTotalVolumeCount
    double hourlyTotalAmount = currentTotalAmount;
    double hourlyTotalVolumeCount = currentTotalVolumeCount;
    double hourlyTotalBuyAmount = currentTotalBuyAmount;
    double hourlyTotalSellAmount = currentTotalSellAmount;
    if (number >= 3600) { // 假设每小时有60分钟，每分钟有60秒，所以3600秒是一小时
        String previousHourlyStr = (String) redisTemplate.opsForList().index("2:", number - 3600);
        if (previousHourlyStr != null) {
            String[] previousHourlyArray = previousHourlyStr.split(":");
            if (previousHourlyArray.length != 4) {
                log.error("数据格式不正确: previousHourlyArray={}", previousHourlyArray);
                return List.of(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
            }
            double previousHourlyTotalAmount = Double.parseDouble(previousHourlyArray[0]);
            double previousHourlyTotalVolumeCount = Double.parseDouble(previousHourlyArray[1]);
            double previousHourlyTotalBuyAmount = Double.parseDouble(previousHourlyArray[2]);
            double previousHourlyTotalSellAmount = Double.parseDouble(previousHourlyArray[3]);
            hourlyTotalAmount = currentTotalAmount - previousHourlyTotalAmount;
            hourlyTotalVolumeCount = currentTotalVolumeCount - previousHourlyTotalVolumeCount;
            hourlyTotalBuyAmount = currentTotalBuyAmount - previousHourlyTotalBuyAmount;
            hourlyTotalSellAmount = currentTotalSellAmount - previousHourlyTotalSellAmount;
        }
    }

    return List.of(
            totalAmount,
            totalVolumeCount,
            totalBuyAmount,
            totalSellAmount,
            minuteTotalAmount,
            minuteTotalVolumeCount,
            minuteTotalBuyAmount,
            minuteTotalSellAmount,
            hourlyTotalAmount,
            hourlyTotalVolumeCount,
            hourlyTotalBuyAmount,
            hourlyTotalSellAmount);
}


@GetMapping("/tradeVolume")
public List<Double> getTradeVolume(@RequestParam int number) {
    // 获取当前下标的元素
    String currentStr = (String) redisTemplate.opsForList().index("1:", number);
    if (currentStr == null) {
        return List.of(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
    }
    String[] currentArray = currentStr.split(":");
    double currentTotalAmount = Double.parseDouble(currentArray[1]);
    double currentTotalVolumeCount = Double.parseDouble(currentArray[0]);

    // 计算totalAmount和totalVolumeCount
    double totalAmount = currentTotalAmount;
    double totalVolumeCount = currentTotalVolumeCount;
    if (number >= 1) {
        String previousStr = (String) redisTemplate.opsForList().index("1:", number - 1);
        if (previousStr != null) {
            String[] previousArray = previousStr.split(":");
            double previousTotalAmount = Double.parseDouble(previousArray[1]);
            double previousTotalVolumeCount = Double.parseDouble(previousArray[0]);
            totalAmount = currentTotalAmount - previousTotalAmount;
            totalVolumeCount = currentTotalVolumeCount - previousTotalVolumeCount;
        }
    }

    // 计算minuteTotalAmount和minuteTotalVolumeCount
    double minuteTotalAmount = currentTotalAmount;
    double minuteTotalVolumeCount = currentTotalVolumeCount;
    if (number >= 60) {
        String previousMinuteStr = (String) redisTemplate.opsForList().index("1:", number - 60);
        if (previousMinuteStr != null) {
            String[] previousMinuteArray = previousMinuteStr.split(":");
            double previousMinuteTotalAmount = Double.parseDouble(previousMinuteArray[1]);
            double previousMinuteTotalVolumeCount = Double.parseDouble(previousMinuteArray[0]);
            minuteTotalAmount = currentTotalAmount - previousMinuteTotalAmount;
            minuteTotalVolumeCount = currentTotalVolumeCount - previousMinuteTotalVolumeCount;
        }
    }

    // 计算hourlyTotalAmount和hourlyTotalVolumeCount
    double hourlyTotalAmount = currentTotalAmount;
    double hourlyTotalVolumeCount = currentTotalVolumeCount;
    if (number >= 3600) { // 假设每小时有60分钟，每分钟有60秒，所以3600秒是一小时
        String previousHourlyStr = (String) redisTemplate.opsForList().index("1:", number - 3600);
        if (previousHourlyStr != null) {
            String[] previousHourlyArray = previousHourlyStr.split(":");
            double previousHourlyTotalAmount = Double.parseDouble(previousHourlyArray[1]);
            double previousHourlyTotalVolumeCount = Double.parseDouble(previousHourlyArray[0]);
            hourlyTotalAmount = currentTotalAmount - previousHourlyTotalAmount;
            hourlyTotalVolumeCount = currentTotalVolumeCount - previousHourlyTotalVolumeCount;
        }
    }

    return List.of(
            totalAmount,
            totalVolumeCount,
            minuteTotalAmount,
            minuteTotalVolumeCount,
            hourlyTotalAmount,
            hourlyTotalVolumeCount);
}



   @GetMapping("/tradeTenStock")
public List getTradeTenStock() {
    String key = "stock_trade_volumes";
    Set<ZSetOperations.TypedTuple<Object>> zSetData = redisTemplate.opsForZSet().reverseRangeWithScores(key, 0, -1);
    if (zSetData != null) {

        return zSetData.stream()
                .map(tuple -> {
                    String value = (String) tuple.getValue();
                    String score = String.format("%d", Math.round(tuple.getScore()));
                    return value + ":" + score;
                })
                .toList();

    }

    return List.of();
}

@GetMapping("/tradeTenPlatform")
public List getTradeTenPlatform() {
    String key = "stock_trade_platform";
    Set<ZSetOperations.TypedTuple<Object>> zSetData = redisTemplate.opsForZSet().reverseRangeWithScores(key, 0, -1);
    if (zSetData != null) {

        return zSetData.stream()
                .map(tuple -> {
                    String value = (String) tuple.getValue();
                    String score = String.format("%d", Math.round(tuple.getScore()));
                    return value + ":" + score;
                })
                .toList();

    }

    return List.of();
}

@GetMapping("/tradeTenPlace")
public List getTradeTenPlace() {
    String key = "stock_trade_place";
    Set<ZSetOperations.TypedTuple<Object>> zSetData = redisTemplate.opsForZSet().reverseRangeWithScores(key, 0, -1);
    if (zSetData != null) {

        return zSetData.stream()
                .map(tuple -> {
                    String value = (String) tuple.getValue();
                    String score = String.format("%d", Math.round(tuple.getScore()));
                    return value + ":" + score;
                })
                .toList();

    }

    return List.of();
}





}